package com.ibm.sc.osp.sample.web.controller;

import com.ibm.sc.osp.base.core.util.StringUtils;
import com.ibm.sc.osp.base.core.util.pagination.PageAdapter;
import com.ibm.sc.osp.base.core.util.pagination.ViewPage;
import com.ibm.sc.osp.common.web.BaseController;
import com.ibm.sc.osp.sample.domain.entity.PfModel;
import com.ibm.sc.osp.sample.service.PfModelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;

@Controller
@RequestMapping(value = "${adminPath}/message")
public class ModelController extends BaseController {
    
    @Value("${adminPath}")
    private String admin;
    
    @Autowired
    private PfModelService service;
    
    @RequiresPermissions("message:model:view")
    @RequestMapping(value = "/listModel")
    public String listModel(
        PfModel PfModel, Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("page", service.findPfModelByPage(new PageAdapter<>(new ViewPage<>(request, response)), PfModel));
        model.addAttribute("PfModel", PfModel);
        return "/message/listModel";
    }
    
    
    /**
     * 删除模板
     */
    @RequestMapping(value = "/deleteModel")
    public String deleteModel(String id, RedirectAttributes redirectAttributes) {
        if (service.deleteModel(id)) {
            addMessage(redirectAttributes, "删除成功！");
        } else {
            addMessage(redirectAttributes, "模板不存在!");
        }
        return "redirect:" + admin + "/message/listModel";
    }
    
    @RequiresPermissions("message:model:edit")
    @RequestMapping(value = "/editModel")
    public String findUserList(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            model.addAttribute("PfModel", service.getPfModel(id));
        }
        return "/message/editModel";
    }
    
    /**
     * 保存模板
     */
    @RequiresPermissions("message:model:edit")
    @RequestMapping(value = "/edit")
    public String editModel(
        PfModel PfModel, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        try {
            String remark = request.getParameter("remark");
            PfModel.setRemarks(remark);
            PfModel.setCreatedDate(new Timestamp(System.currentTimeMillis()));
            PfModel.setDelFlag("0");
            if (!StringUtils.isNotBlank(PfModel.getId())) {
                PfModel.setStatus("0");
            }
            service.savePfModel(PfModel);
            addMessage(redirectAttributes, "保存成功！");
        } catch (Exception e) {
            addMessage(redirectAttributes, "保存失败！错误信息：" + e.getMessage());
        }
        return "redirect:" + admin + "/message/listModel";
    }
    
    @ResponseBody
    @RequestMapping(value = "/check")
    public String check(String name, String method) {
        return service.checkPfModel(name, method) ? "0" : "1";
    }
}
